import jwtToken from '@/helpers/jwt'
import axios from '@/helpers/http'
export default {
  actions: {
    loginRequest ({dispatch}, formData) {
      return axios.post('/api/v2/login', formData).then(response => {
        dispatch('loginSuccess', response.data.data)
      })
    },
    loginSuccess ({dispatch}, tokenResponse) {
      jwtToken.setToken(tokenResponse.access_token)
      jwtToken.setAuthId(tokenResponse.auth_id)
      dispatch('setAuthUser')
    },
    logoutRequest ({dispatch}) {
      return axios.post('/api/v2/logout')
        .then(response => {
          jwtToken.removeToken()
          dispatch('unsetAuthUser')
        })
    }
  }
}
